package com.shouer.doc.ui.neworder.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shouer.doc.R;
import com.shouer.doc.common.util.DateFormatUtil;
import com.shouer.doc.ui.neworder.model.HealthRecord;
import com.shouer.doc.ui.neworder.view.INewOrdView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by YYY-Work on 2017/8/7.
 */

public class HealthRecordAdapter extends RecyclerView.Adapter<HealthRecordVH> implements View.OnClickListener{

    private Context mContext;
    private List<HealthRecord> mHealthRecords;
    private LayoutInflater layoutInflater;
    private INewOrdView newOrdView;

    public HealthRecordAdapter(Context context, List<HealthRecord> healthRecords, INewOrdView newOrdView) {
        this.mContext = context;
        this.mHealthRecords = healthRecords;
        layoutInflater = LayoutInflater.from(context);
        this.newOrdView = newOrdView;
    }


    @Override
    public HealthRecordVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.fragment_new_health_report_item_layout, null);
        return new HealthRecordVH(itemView);
    }

    @Override
    public void onBindViewHolder(HealthRecordVH holder, int position) {

        HealthRecord record=mHealthRecords.get(position);

        holder.titleView.setText(TextUtils.isEmpty(record.getItemName())?"":record.getItemName());

        try {
            Date createDate=DateFormatUtil.stringToDate(record.getUpdateDate().replace("T","\b"),"yyyy-MM-dd\bHH:mm:ss");
            holder.dateView.setText(DateFormatUtil.dateToString(createDate,"yyyy-MM-dd"));
        } catch (Exception e) {
            Log.d(HealthRecordAdapter.class.getSimpleName(), e.toString());
            holder.dateView.setText("");
        }

        holder.patName.setText(TextUtils.isEmpty(record.getPatient().getPatName())?"":record.getPatient().getPatName());
        holder.patSex.setText(record.getPatient().getPatSex()==0?"女":"男");

        try {
            Date createDate=DateFormatUtil.stringToDate(record.getPatient().getPatBirthday().replace("T","\b"),"yyyy-MM-dd\bHH:mm:ss");
            holder.patBirDate.setText(DateFormatUtil.dateToString(createDate,"yyyy-MM-dd"));
        } catch (Exception e) {
            Log.d(HealthRecordAdapter.class.getSimpleName(), e.toString());
            holder.patBirDate.setText("");
        }

        holder.receiveBtn.setTag(R.id.list_position,position);
        holder.receiveBtn.setOnClickListener(this);

    }

    @Override
    public int getItemCount() {
        if (null == mHealthRecords) {
            return 0;
        } else {
            return mHealthRecords.size();
        }
    }

    public void refreshHealthRecordList(List<HealthRecord> healthRecords) {
        Log.i(HealthRecordAdapter.class.getSimpleName(), "refreshHealthRecordList");
        this.mHealthRecords = healthRecords;
        notifyDataSetChanged();
    }

    public void attachToHealthRecordList(List<HealthRecord> healthRecords) {

        Log.i(HealthRecordAdapter.class.getSimpleName(), "attachToHealthRecordList");
        if (null != healthRecords) {
            if (null == mHealthRecords) {
                mHealthRecords = new ArrayList<>(0);
            }

            mHealthRecords.addAll(healthRecords);
            notifyDataSetChanged();
        }
    }


    public void removeHealthRecord(HealthRecord record) {
        try {
            mHealthRecords.remove(record);
            notifyDataSetChanged();
        } catch (Exception e) {
            Log.e(HealthRecordAdapter.class.getSimpleName(), "removeHealthRecord Exception:" + e.toString());
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.action_btn:
                int position= (int) v.getTag(R.id.list_position);
                HealthRecord healthRecord=getItemByPosition(position);
                if (null!=healthRecord)
                {
                    newOrdView.receiveHealthRecord(healthRecord);
                }
                break;
        }

    }

    private HealthRecord getItemByPosition(int position)
    {
        try {
            HealthRecord healthRecord=mHealthRecords.get(position);
            return healthRecord;
        }catch (Exception e)
        {
            Log.e(HealthRecordAdapter.class.getSimpleName(),"getItemByPosition------:"+e);
            return null;
        }

    }


    @Override
    public long getItemId(int position)
    {
        return position;
    }
}
